﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using HYFamilyLibDataManager;

namespace HYFamilyLibManager.FamilyLibModule
{
	public partial class DlgProgress : System.Windows.Forms.Form
	{
		public DlgProgress(List<FamilyLibDatabase> dbs, UIApplication uiApp)
		{
			this.InitializeComponent();
			this.mUiApp = uiApp;
			this.mDbs = dbs;
			int num = 0;
			foreach (FamilyLibDatabase familyLibDatabase in this.mDbs)
			{
				num += familyLibDatabase.FamilyTable.Count(CheckType.All);
			}
			this.pgbTotal.Maximum = num;
		}

		private void DlgProgress_Load(object sender, EventArgs e)
		{
		}

		private void button1_Click(object sender, EventArgs e)
		{
		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show("确定终止升级吗？", "终止", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
			{
				base.Close();
			}
		}

		private void DlgProgress_Shown(object sender, EventArgs e)
		{
			foreach (FamilyLibDatabase familyLibDatabase in this.mDbs)
			{
				int maximum = familyLibDatabase.FamilyTable.Count(CheckType.All);
				this.pgbDb.Maximum = maximum;
				this.pgbDb.Value = 0;
				SqliteDatabase sqliteDatabase = ((SqliteDatabase)familyLibDatabase).CopyForUpdate();
				if (sqliteDatabase != null)
				{
					foreach (DbFamily dbFamily in sqliteDatabase.FamilyTable.GetAll())
					{
						ProgressBar progressBar = this.pgbDb;
						int value = progressBar.Value;
						progressBar.Value = value + 1;
						this.lblDb.Text = string.Format("正在升级{0}库，已完成{1}%。", sqliteDatabase.ToString(), (this.pgbDb.Value * 100 / this.pgbDb.Maximum).ToString());
						ProgressBar progressBar2 = this.pgbTotal;
						value = progressBar2.Value;
						progressBar2.Value = value + 1;
						this.lblTotal.Text = string.Format("总共已完成{0}%。", (this.pgbTotal.Value * 100 / this.pgbTotal.Maximum).ToString());
						try
						{
							dbFamily.Update(this.mUiApp.Application);
							dbFamily.Version = this.mUiApp.Application.VersionNumber;
							sqliteDatabase.FamilyTable.Update(dbFamily);
						}
						catch (Exception ex)
						{
							MessageBox.Show(ex.Message);
						}
					}
					sqliteDatabase.SubjectTable.SetVersion(this.mUiApp.Application.VersionNumber);
				}
				else
				{
					this.pgbDb.Value += familyLibDatabase.FamilyTable.Count(CheckType.All);
					this.lblDb.Text = string.Format("正在升级{0}库，已完成{1}%。", familyLibDatabase.ToString(), (this.pgbDb.Value * 100 / this.pgbDb.Maximum).ToString());
					this.pgbTotal.Value += familyLibDatabase.FamilyTable.Count(CheckType.All);
					this.lblTotal.Text = string.Format("总共已完成{0}%。", (this.pgbTotal.Value * 100 / this.pgbTotal.Maximum).ToString());
				}
			}
			MessageBox.Show("升级完成！");
			base.Close();
		}

		private List<FamilyLibDatabase> mDbs = new List<FamilyLibDatabase>();

		public UIApplication mUiApp;
	}
}
